# -*- coding: utf-8 -*-

# -------------------------------
# Author    : linlingwei
# Email     : 951035650@qq.com
# File Name : fTongue.py
# Date      : 2019/9/20 
# IDE       : PyCharm
# Version   : 1.0.1
# -------------------------------

import traceback
import maya.cmds as mc
import maya.mel as mel

import configs.constants.facial_constant as fc
import configs.constants.global_constant as gc
import configs.constants.suffix_constant as sc
import configs.constants.nodeType_constant as ntc
import configs.constants.attribute_constant as ac

import scripts.commonCmds as con
reload(con)
import scripts.curveCmds as cr
import scripts.deformerCmds as dc
reload(dc)
import rig.facial.fCore as fCore
reload(fCore)

class Tongue(fCore.FacialCore):
    def __init__(self):
        super(Tongue,self).__init__(fc.tongue)
        self.transform_init()
    
    def create(self):
        try:
            self.driver_loc()
            self.hide_doDelete()
        except:
            traceback.print_exc()
        return
    
    def driver_loc(self):
        
        tmp_locs = mc.ls(con.lname(fc.tongue,"tmp","*",sc.sLoc))
        loc_pat = None
        ctr_pat = None
        
        jot_list = list()
        for i,tl in enumerate([tmp_locs[0]]+tmp_locs):
            _lname = con.lname(fc.tongue,i)
            if i == 0:
                _lname = con.lname(fc.tongue,fc.root)
            loc = mc.spaceLocator(name = con.lname(_lname,sc.sLoc))[0]
            mc.delete(mc.pointConstraint(tl,loc,mo=False))
            jot = mc.createNode(ntc.tJoint,name=con.lname(_lname,sc.sJot),parent=loc)
            mc.setAttr(jot+".radius",0.1)
            jot_list.append(jot)
            if loc_pat:
                mc.parent(loc,loc_pat)
            else:
                mc.parent(loc,self.local_static)
            loc_pat = loc
            con.hideShape(loc)
            loc_grp = con.emptyGroup(loc)
            ctr = cr.objToControl(loc,name = con.lname(_lname),colorId=17)
            if i == 0:
                cr.replaceShape(ctr,"cube",colorId=13)
            cr.rotate(ctr,90)
            cr.scaleXYZ(ctr,0.15)
            if ctr_pat:
                mc.parent(ctr,ctr_pat)
            else:
                mc.parent(ctr,self.local_toParent)
            ctr_pat = ctr
            ctr_grp = con.emptyGroup(ctr)
            for _c,_l in zip(ctr_grp[2:]+[ctr],loc_grp[2:]+[loc]):
                for _at in ["t","r","s"]:
                    mc.connectAttr(con.lAttr(_c,_at),con.lAttr(_l,_at))
            
        mc.sets(jot_list,include=self.local_sets)
        
        
        return

if __name__ == '__main__':
    pass